on unlockfile(encryptedFile, eReaderDeDRMPath, encryptionNameKey, encryptionKey)
	set encryptedFilePath to POSIX path of file encryptedFile
	tell application "Finder" to 
		set parent_folder to (container of file encryptedFile) as text
	tell application "Finder" to set fileName to (name of file encryptedFile) as text
	set unlockedFilePath to POSIX path of file (parent_folder & "Unlocked_" & fileName)
	set shellcommand to "python \"" & eReaderDeDRMPath & "\" \"" & encryptedFilePath & "\" \"" & unlockedFilePath & "\" \"" & encryptionNameKey & "\" " & encryptionKey
	try
		--with timeout of 5 seconds
		--display dialog "About to Unlock " & fileName buttons {"Unlock"} default button 1 giving up after 1
		--end timeout
	end try
	set result to do shell script shellcommand
	try
		--with timeout of 5 seconds
		--display dialog "Result" default answer result buttons ("OK") default button 1 --giving up after 2
		--end timeout
	end try
end unlockfile

on unlockfolder(encryptedFolder, eReaderDeDRMPath, encryptionNameKey, encryptionKey)
	tell application "Finder" to set encryptedFileList to (every file in folder encryptedFolder) whose (name extension is "pdb")
	tell application "Finder" to set encryptedFolderList to (every folder in folder encryptedFolder)
	repeat with this_item in encryptedFileList
		unlockfile(this_item as text, eReaderDeDRMPath, encryptionNameKey, encryptionKey)
	end repeat
	repeat with this_item in encryptedFolderList
		unlockfolder(this_item as text, eReaderDeDRMPath, encryptionNameKey, encryptionKey)
	end repeat
end unlockfolder

on run
	set eReaderDeDRMPath to POSIX path of file ((path to me as text) & "Contents:Resources:eReaderDeDRM.py")
	set encryptedFolder to choose folder with prompt "Please choose the folder of encrypted eReader files."
	set encryptionNameKey to (display dialog "Enter Name key for encrypted eReader files." default answer "MR PAUL M P DURRANT" buttons {"Cancel", "OK"} default button 2)
	set encryptionKey to (display dialog "Enter Number key for encrypted eReader files." default answer "89940827" buttons {"Cancel", "OK"} default button 2)
	set encryptionNameKey to text returned of encryptionNameKey
	set encryptionKey to text returned of encryptionKey
	unlockfolder(encryptedFolder, eReaderDeDRMPath, encryptionNameKey, encryptionKey)
end run

on open some_items
	set eReaderDeDRMPath to POSIX path of file ((path to me as text) & "Contents:Resources:eReaderDeDRM.py")
	set encryptionNameKey to (display dialog "Enter Name key for encrypted eReader files." default answer "MR PAUL M P DURRANT" buttons {"Cancel", "OK"} default button 2)
	set encryptionKey to (display dialog "Enter Number key for encrypted eReader files." default answer "89940827" buttons {"Cancel", "OK"} default button 2)
	set encryptionNameKey to text returned of encryptionNameKey
	set encryptionKey to text returned of encryptionKey
	repeat with this_item in some_items
		if (folder of (info for this_item) is true) then
			unlockfolder(this_item as text, eReaderDeDRMPath, encryptionNameKey, encryptionKey)
		else
			tell application "Finder" to set item_extension to name extension of file this_item
			if item_extension is "pdb" then
				unlockfile(this_item as text, eReaderDeDRMPath, encryptionNameKey, encryptionKey)
			end if
		end if
	end repeat
end open

